<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <!--<el-form-item>-->
      <!--<el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>-->
      <!--</el-form-item>-->
      <el-form-item>
        <!--<el-button @click="getDataList()">查询</el-button>-->
        <el-button
          v-if="isAuth('train:traincourse:save')"
          type="primary"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          v-if="isAuth('train:traincourse:delete') && tabIndex==1"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name="first"></el-tab-pane>
      <el-tab-pane label="未发布" name="second"></el-tab-pane>
      <el-tab-pane label="已发布" name="third"></el-tab-pane>
      <el-tab-pane label="培训中" name="fourth"></el-tab-pane>
      <el-tab-pane label="培训结束" name="fifth"></el-tab-pane>
    </el-tabs>
    <el-table
      :data="dataList"
      row-key="id"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        v-if="isAuth('train:traincourse:delete') && parseInt(tabIndex)===1"
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <!--<el-table-column align="center" type="index" header-align="center" width="80" label="排序"></el-table-column>-->
      <el-table-column type="index" header-align="center" align="center" width="80px" label="序号"></el-table-column>
      <el-table-column
        prop="status"
        label="发布状态"
        width="100px"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <!--状态:0-未发布  1-发布，报名中 2-报名结束   3-培训结束-->
          <el-button
            type="primary"
            v-if="parseInt(scope.row.status) === 0"
            @click="evaluateHandle(scope.row.id)"
          >发布</el-button>
          <el-tag v-if="parseInt(scope.row.status) === 1">已发布</el-tag>
          <el-tag v-if="parseInt(scope.row.status) === 2">培训中</el-tag>
          <el-tag v-if="parseInt(scope.row.status) === 3">培训结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        min-width="120px"
        label="培训名称"
      ></el-table-column>
      <el-table-column label="报名时间" header-align="center" min-width="160" align="center">
        <template slot-scope="scope">
          <span>
            {{scope.row.entryStartTime}}
            <br />至
            <br />
            {{scope.row.entryEndTime}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="培训时间" width="160" header-align="center" align="center">
        <template slot-scope="scope">
          <span>
            {{scope.row.trainStartTime}}
            <br />至
            <br />
            {{scope.row.trainEndTime}}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="mainImg" label="封面" header-align="center" width="200px" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.mainImgUrl" width="180px" height="100px" />
        </template>
      </el-table-column>
      <el-table-column
        prop="trainTeacher"
        label="授课老师"
        min-width="90"
        header-align="center"
        align="center"
      ></el-table-column>
      <!--<el-table-column prop="trainHours" label="授课课时" header-align="center" align="center"></el-table-column>-->
      <el-table-column
        prop="trainPlace"
        label="培训地点"
        width="200"
        height="100"
        header-align="center"
        show-overflow-tooltip
        align="center"
      ></el-table-column>
      <el-table-column label="考试名称" header-align="center" align="center">
        <template slot-scope="scope">{{scope.row.examName || '---'}}</template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200px" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.id,scope.row.status)">修改</el-button>
          <el-button
            v-if="parseInt(scope.row.status) !== 0"
            type="text"
            @click="applicationSituationHandle(scope.row.id)"
          >报名情况</el-button>
          <el-button
            v-if="parseInt(scope.row.status) === 1"
            type="text"
            @click="registrationHandle(scope.row.id)"
          >为他报名</el-button>
          <el-button
            v-if="parseInt(scope.row.status) !== 0"
            type="text"
            @click="attendanceSheet(scope.row.id)"
          >考勤管理</el-button>
          <el-button
            v-if="parseInt(scope.row.status) === 3"
            type="text"
            @click="trainingRecordsHandle(scope.row.id)"
          >培训档案</el-button>
          <el-button
            v-if="parseInt(scope.row.status) === 3"
            type="text"
            @click="evaluationHandle(scope.row.id)"
          >查看评价</el-button>
          <el-button
            v-if="parseInt(scope.row.status) === 3"
            type="text"
            @click="trainteacherHandle(scope.row.id)"
          >讲师评价</el-button>
          <el-button
            v-if="parseInt(scope.row.isExam) === 0"
            type="text"
            @click="relattionHandle(scope.row.id)"
          >关联考试</el-button>
          <el-button
            v-if="parseInt(scope.row.status) === 0"
            type="text"
            @click="deleteHandle(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />

    <!-- 弹窗, 报名情况 -->
    <application-situation v-if="ApplicationSituationVisible" ref="ApplicationSituation" />

    <!--弹窗,考勤信息-->
    <attendance v-if="attendanceVisible" ref="attendance" />

    <!-- 查看评价  -->
    <comment v-if="commentVisible" ref="comments" />

    <!--  培训档案 -->
    <trainingRecords v-if="trainingRecordsVisible" ref="trainingRecords" />

    <!--查看-->
    <see-details v-if="seeDetailsVisible" ref="SeeDetails" />

    <!--  讲师评价 -->
    <train-teacher v-if="trainTeacherVisible" ref="trainteachers" />

    <!--关联考试-->
    <train-relation v-if="trainRelationVisible" ref="trainRelation" @refreshDataList="getDataList" />

    <!--为他报名-->
    <train-registration v-if="trainRegistrationVisible" ref="trainRegistration" />
  </div>
</template>

<script>
import AddOrUpdate from './traincourse-add-or-update'
import Attendance from './train-checkinrecord'
import ApplicationSituation from './applicationSituation'
import ApplicationSituationDetail from './applicationSituationDetail'
import comment from './reviews-user'
import TrainingRecords from './trainingRecords'
import TrainTeacher from './trainteacher'
import TrainRelation from './train-relation'
import TrainRegistration from './train-registration'

export default {
  data() {
    return {
      activeName: 'first',
      tabIndex: 0,
      dataForm: {
        key: '',
        trainType: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      trainTeacherVisible: false,
      ApplicationSituationVisible: false,
      relevantUsersVisible: false,
      trainRelationVisible: false,
      trainRegistrationVisible: false,
      commentVisible: false,
      seeDetailsVisible: false,
      qrcodVisible: false,
      trainingRecordsVisible: false,
      relevantVisible: false,
      attendanceVisible: false
    }
  },
  components: {
    TrainRelation,
    AddOrUpdate,
    ApplicationSituation,
    Attendance,
    ApplicationSituationDetail,
    comment,
    TrainingRecords,
    TrainTeacher,
    TrainRegistration
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      // let date = new Date()
      // let timeStr = date.getTime()
      this.$http({
        url: this.$http.adornUrl('/train/admin/train/list'),
        method: 'post',
        data: {
          page: this.pageIndex,
          limit: this.pageSize,
          status: this.tabIndex - 1
        }
      }).then(res => {
        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
          // this.dataList.forEach(function(data) {
          //   let endtime = new Date(data.trainEndTime).getTime();
          //   let starttime = new Date(data.trainStartTime).getTime();
          //   if (data.status == 1 && endtime > timeStr > starttime) {
          //     data.status == 2;
          //   }
          //   if (data.status == 1 && endtime < timeStr) {
          //     data.status == 3;
          //   }
          // });
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },

    // tab切换
    handleClick(e) {
      this.tabIndex = e.index
      this.getDataList((this.pageIndex = 1))
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id, status) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, status)
      })
    },
    // 发布培训
    evaluateHandle(id) {
      var that = this
      that
        .$confirm(
          '名师讲习发布后将无法对信息进行修改，是否继续发布？',
          '提示',
          // eslint-disable-next-line
          {
            // eslint-disable-next-line
            confirmButtonText: '发布',
            // eslint-disable-next-line
            cancelButtonText: '取消',
            // eslint-disable-next-line
            type: 'warning'
            // eslint-disable-next-line
          }
        )
        .then(() => {
          that
            .$http({
              url: that.$http.adornUrl(
                '/train/admin/train/updatetraincoursestatus'
              ),
              method: 'post',
              data: { trainId: id }
            })
            .then(data => {
              if (data && data.code === 200) {
                that.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                that.getDataList()
              } else {
                that.$message.warning(data.msg)
              }
            })
        })
    },

    // 查看报名情况
    applicationSituationHandle(id) {
      this.ApplicationSituationVisible = true
      this.$nextTick(() => {
        this.$refs.ApplicationSituation.init(id)
      })
    },

    // 查看考勤
    attendanceSheet(id) {
      this.attendanceVisible = true
      this.$nextTick(() => {
        this.$refs.attendance.init(id)
      })
    },
    // 一期一档
    trainingRecordsHandle(id) {
      this.trainingRecordsVisible = true
      this.$nextTick(() => {
        this.$refs.trainingRecords.init(id)
      })
    },
    // 关联用户
    relevantUsersHandle(id) {
      this.relevantUsersVisible = true
      this.$nextTick(() => {
        this.$refs.relevantUsers.init(id)
      })
    },
    // 关联用户2
    relevantHandle(id) {
      this.relevantVisible = true
      this.$nextTick(() => {
        this.$refs.relevant.init(id)
      })
    },
    // 查看评价
    evaluationHandle(id) {
      this.commentVisible = true
      this.$nextTick(() => {
        this.$refs.comments.init(id)
      })
    },
    // 讲师评价
    trainteacherHandle(id) {
      this.trainTeacherVisible = true
      this.$nextTick(() => {
        this.$refs.trainteachers.init(id)
      })
    },
    // 关联考试
    relattionHandle(id) {
      this.trainRelationVisible = true
      this.$nextTick(() => {
        this.$refs.trainRelation.init(id)
      })
    },
    // 为他报名
    registrationHandle(id) {
      this.trainRegistrationVisible = true
      this.$nextTick(() => {
        this.$refs.trainRegistration.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      // let ids = id ? [id] : this.dataListSelections.map(item => item.id)
      // let ids = this.dataListSelections.map(item => item.id)
      this.$confirm(
        // `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        `确定要进行删除操作吗？`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl('/train/admin/train/delete'),
          method: 'post',
          data: {
            ids: id || this.dataListSelections.map(item => item.id).join(',')
          }
        }).then(res => {
          if (res && res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    }
  }
}
</script>
